package com.business_clean.ui.fragment.main;

import static com.business_clean.app.config.Constant.TYPE_INIT_DATA;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import com.business_clean.app.App;
import com.business_clean.app.base.BaseFragment;
import com.business_clean.data.mode.todo.TodoList;
import com.business_clean.databinding.FragmentTodoBinding;
import com.business_clean.ui.adapter.viewpager2.FragmentLazyPagerAdapter;
import com.business_clean.ui.fragment.todo.TodoItemFragment;
import com.business_clean.viewmodel.request.TodoViewModel;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.tabs.TabLayout;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class TodoFragment extends BaseFragment<TodoViewModel, FragmentTodoBinding> {

    private boolean isLazyLoad = false; //是否已经懒加载了

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        ArrayList<Fragment> mFragments = new ArrayList<>();
        TodoItemFragment fragment1 = new TodoItemFragment(1);
        TodoItemFragment fragment2 = new TodoItemFragment(2);
        TodoItemFragment fragment3 = new TodoItemFragment(3);
        mFragments.add(fragment1);
        mFragments.add(fragment2);
        mFragments.add(fragment3);

        List<String> stringList = new ArrayList<>();

        stringList.add("待我处理");
        stringList.add("我已处理");
        stringList.add("我创建的");

        getMDatabind().viewpagerTodo.setAdapter(new FragmentLazyPagerAdapter(getChildFragmentManager(), mFragments, stringList));
        getMDatabind().viewpagerTodo.setOffscreenPageLimit(mFragments.size());
        getMDatabind().tabLayout.setupWithViewPager(getMDatabind().viewpagerTodo);

    }

    @Override
    public void initObserver() {
        getMDatabind().tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                App.appViewModelInstance.getRefreshTodo().setValue(true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        //监听数字
        App.appViewModelInstance.getRefreshTodoTotal().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer number) {
                TabLayout.Tab tab = getMDatabind().tabLayout.getTabAt(0);
                if (tab != null) {
                    if (number > 0) {
                        BadgeDrawable badge = tab.getOrCreateBadge();
                        badge.setBackgroundColor(Color.RED);
                        badge.setMaxCharacterCount(3);
                        badge.setNumber(number);
                        badge.setBadgeTextColor(Color.WHITE);
                    } else {
                        tab.removeBadge();
                    }
                }
            }
        });

    }

    @Override
    public void lazyLoadData() {
        isLazyLoad = true;//记录是否已经懒加载了
        requestOne();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isLazyLoad) {
            requestOne();
        }
    }

    @Override
    public void onRequestSuccess() {
        mViewModel.getTodoList().observe(this, new Observer<TodoList>() {
            @Override
            public void onChanged(TodoList todoList) {
                int number = Integer.valueOf(todoList.getTotal());//统一去执行 监听
                App.appViewModelInstance.getRefreshTodoTotal().setValue(number);
            }
        });
    }


    private void requestOne() {
        mViewModel.requestTodoList(1, 1);
    }

}
